package org.batatasframework.support.database.values;

import java.sql.ResultSet;
import java.text.DateFormat;
import java.util.Date;

import org.batatasframework.support.database.sql.field.SelectFragment;


public class DateValueHolder extends AbstractSingleValueHolder{

	protected final DateFormat format;
	
	public DateValueHolder(SelectFragment field, String alias, Object format) {
		super(field, alias);
		if(format instanceof DateFormat){
			this.format = (DateFormat)format;
		}else{
			this.format = null;
		}
	}

	@Override
	public FieldValue read(ResultSet rs) throws Exception {
		return new DateValue(rs.getDate(alias));
	}

	@Override
	public StatisticsTotal createStatistics() {
		return null;
	}
	
	protected class DateValue extends AbstractValue<Date>{
		
		public DateValue(Date value) {
			super(value);
		}

		@Override
		public Object targetValue() {
			if(null == value) return "";
			if(null == format) return value;
			return format.format(value);
		}
	}
}
